﻿using AutoMapper;
using ERPPublic;
using MediatR;
using Newtonsoft.Json;
using ProductionManagement.Application.Command.DispatchCommand;
using ProductionManagement.Domain.Dtos.Dispathch;
using ProductionManagement.Dto;
using ProductionManagement.Infrstructre.lnterface;
using SqlSugar;
using System.Runtime.InteropServices.JavaScript;
using static ERPPublic.ParseAPI;
namespace ProductionManagement.Application.CommandHandler.DIspatchCommandHandlers
{
    public class BlikDispatchCommandHandlers : IRequestHandler<BlikDispatchCommand, List<ShowDispathchDTO>>
    {
        private readonly IDispatchRepository _dispatch;
        private readonly IDisProductRepository _disProduct;
        private readonly IDisMaterialRepository _disMaterial;
        private readonly IMapper _mapper;
        ISqlSugarClient db;
        public BlikDispatchCommandHandlers(IDispatchRepository dispatch, IDisProductRepository disProduct, IDisMaterialRepository disMaterial, IMapper mapper, ISqlSugarClient db)
        {
            _dispatch = dispatch;
            _disProduct = disProduct;
            _disMaterial = disMaterial;
            _mapper = mapper;
            this.db = db;
        }


        public async Task<List<ShowDispathchDTO>> Handle(BlikDispatchCommand request, CancellationToken cancellationToken)
        {
            List<Products> products = await GetProductList("", "", 1, 1000);
            var dispatch = (from f in await _dispatch.GetAll() select f).ToList();
            var disProduct = (from fp in await _disProduct.GetAll() select fp).ToList();
            var disMaterial = (from fp in await _disMaterial.GetAll() select fp).ToList();
            var list = (from a in dispatch
                        join p in products on a.ProductId equals p.ProductId
                        where a.IsDel == false&&a.DispatchId==request.DispatchId
                        select new ShowDispathchDTO
                        {
                            DispatchId = a.DispatchId,
                            DNumber = a.DNumber,
                            DisTopic = a.DisTopic,
                            DPickState = a.DPickState,
                            DMaterState = a.DMaterState,
                            DState = a.DState,
                            WarehouseStorageId = a.WarehouseStorageId,
                            ManpowerId = a.ManpowerId,
                            ManpowerName = a.ManpowerName,
                            ProductId = a.ProductId,
                            PNumber = p.PNumber,
                            PName = p.PName,
                            PSpec = p.PSpec,
                            PUnit = p.PUnit,
                            DAlreadyNum = a.DAlreadyNum,
                            DThisNum = a.DThisNum,
                            AddName = a.AddName,
                            AddTime = a.AddTime,
                            MSubject = a.MSubject,
                            DispatchProducts = (from b in disProduct
                                                join p in products on a.ProductId equals p.ProductId
                                                where b.DNumber == a.DNumber
                                                select new DispatchProductss
                                                {
                                                    DispatchProductId = b.DispatchProductId,
                                                    DNumber = b.DNumber,
                                                    ProductId = b.ProductId,
                                                    PName = p.PName,
                                                    PNumber = p.PNumber,
                                                    PSpec = p.PSpec,
                                                    PUnit = p.PUnit,
                                                    OrderNum = b.OrderNum,
                                                    DThisNum = b.DThisNum,
                                                    DPStartDate = b.DPStartDate,
                                                    DPEndDate = b.DPEndDate,
                                                    DDeliveryDate = b.DDeliveryDate,
                                                }).ToList(),
                            DisMaterials = (from c in disMaterial
                                            where a.DNumber == c.DNumber
                                            select new DisMaterialss
                                            {
                                                DisMaterialId = c.DisMaterialId,
                                                DNumber = c.DNumber,
                                                MaterialId = c.MaterialId,
                                                MSubject = c.MSubject,
                                                WarehouseStorageId = c.WarehouseStorageId,
                                                MPNumber = c.MPNumber,
                                                MPName = c.MPName,
                                                MPSpec = c.MPSpec,
                                                MPUnit = c.MPUnit,
                                                PRatio = c.PRatio,
                                                PStock = c.PStock,
                                                NeedNum = c.NeedNum,
                                                ArrivalDate = c.ArrivalDate,
                                                Remark = c.Remark

                                            }).ToList()



                        }).ToList();



            return list;



        }


        /// <summary>
        /// 显示产品信息
        /// </summary>
        /// <param name="PName"></param>
        /// <param name="PNumber"></param>
        /// <param name="pindex"></param>
        /// <param name="psize"></param>
        /// <returns></returns>
        public async Task<dynamic> GetProductList(string? PName, string? PNumber, int pindex, int psize)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1MCIsInNpZCI6IjE4NjcwOTU3MzQ0NjIwNTg0OTYiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMTksMTkiLCJqdGkiOiI1MGYyMWRmZS0yNWQ3LTQ1OTQtOTg1Yi0wZTM2MDhhZjJiNmYiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi5LuY5rSb5ZiJIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0._wh23n1M7asMGST9NyeZj-5rk5ICAiOFv6yDk6e2o7Y";
            HttpClientWrapper cilent = new HttpClientWrapper(token);

            string chanpin = "http://116.205.121.197:8000/erpcp/api/Product/ProductShow?PName=" + PName + "&PNumber=" + PNumber + "&ProductTypeId=" + 0 + "&PageIndex=" + pindex + "&PageSize=" + psize;

            string postReponse = await cilent.GetAsync(chanpin);
            var json = JsonConvert.DeserializeObject<ApiResultList<Products>>(postReponse);
            return json.data.list;
        }
        //产品
        public class Products
        {
            /// <summary>
            /// 产品Id
            /// </summary>
            [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
            public int ProductId { get; set; }
            /// <summary>
            /// 标识
            /// </summary>

            public int? PCharacteristic { get; set; }
            /// <summary>
            /// 产品编号
            /// </summary>
            [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
            public string? PNumber { get; set; }
            /// <summary>
            /// 产品名称
            /// </summary>
            /// 
            [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
            public string? PName { get; set; }
            /// <summary>
            /// 规格型号
            /// </summary>
            /// 
            [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
            public string? PSpec { get; set; }
            /// <summary>
            /// 单位
            /// </summary>
            /// 
            [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
            public string? PUnit { get; set; }

            /// <summary>
            /// 产品分类ID
            /// </summary>

            public int ProductTypeId { get; set; }




        }



    }
}
